package com.example.lianjuan.ui.custom;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.RecyclerView;

import com.example.lianjuan.Utils.LogUtils;

/**
 * 解决SmartRefresh刷新和NestedScrollView滑动冲突
 *
 * @author 81067
 */
public class HomePagerNestedScrollView extends NestedScrollView {
    private int scrollHigh = 0;
    private int headHeight = 0;

    public HomePagerNestedScrollView(@NonNull Context context) {
        super(context);
    }

    public HomePagerNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public HomePagerNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onNestedPreScroll(@NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {
        if (headHeight > this.scrollHigh) {
        LogUtils.d(this, "dy---->" + dy);
            scrollBy(dx, dy);
            consumed[0] = dx;
            consumed[1] = dy;
        }
        super.onNestedPreScroll(target, dx, dy, consumed, type);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        //View的滑动距离
        this.scrollHigh = t;
        LogUtils.d(this, "t---->" + t);
        super.onScrollChanged(l, t, oldl, oldt);
    }

    /**
     * 设置需要的高度
     *
     * @param headHeight
     */
    public void setHeadHigh(int headHeight) {
        this.headHeight = headHeight;
    }

}
